import {BookModel} from '../../models/book.js'
import { ClassicModel } from '../../models/classic.js'
let bookModel = new BookModel()
let classicModel = new ClassicModel()
Page({
  data: {
    authorized:false,
    userInfo:null,
    bookCount:0,
    classics:null
  },
  onLoad: function (options) {
    this.userAuthorized()
    this.getMyBookCount()
    this.getMyFavor()
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },
  userAuthorized(){
    wx.getSetting({
      success:res=>{
        if (res.authSetting['scope.userInfo']){
          wx.getUserInfo({
            success:res=>{
              console.log(res)
              this.setData({
                authorized:true,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },
  onGetUserInfo(e){
    const userInfo=e.detail.userInfo
    if (userInfo){
      this.setData({
        authorized: true,
        userInfo: userInfo
      })
    }
  },
  onJumpAbout(e){
    wx.navigateTo({
      url: '../about/about'
    })
  },
  onStudy(e){
    wx.navigateTo({
      url: '../course/course'
    })
  },
  getMyBookCount(){
    bookModel.getMybookCount().then(res=>{
      console.log(res)
      this.setData({
        bookCount:res.count
      })
    })
  },
  getMyFavor(){
    classicModel.getMyFavor(res=>{
      console.log(res)
      this.setData({
        classics:res
      })
    })
  }
})